package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.AllocationApplyBean;
import com.txsj.scan.bean.AllocationApplyDetailsBean;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.bean.EpcTagBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.GZIPUtils;
import com.txsj.scan.utils.GsonFactory;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.IAllocationApplyDetailsView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AllocationApplyDetailsPresenter extends BasePresenter<IAllocationApplyDetailsView> {

    public AllocationApplyDetailsPresenter(IAllocationApplyDetailsView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }


    public void getFormDetails(String bill_id) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("bill_id", bill_id);//调出仓库id 默认是零
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).getAllocationApplyDetails(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResult<AllocationApplyDetailsBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResult<AllocationApplyDetailsBean> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData()) {
                                view.loadSuccess(result.getData());
                            } else {
                                view.loadFailed("暂无数据");
                            }
                        } else {
                            view.loadFailed("请求失败，请稍后重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadFailed("请求异常了");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void setAllocationFromStates(String status,String reson) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("employee_id",PreferenceUtil.getInstance().getString(PreferenceUtil.EMPLOYEEID, ""));
        params.put("employee_name",PreferenceUtil.getInstance().getString(PreferenceUtil.EMPLOYEENAME, ""));
        params.put("check_status",status);
        params.put("checkReson",reson);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数

        RetrofitManager.getDefault().create(ApiServer.class).setAllocationFromStates(RetrofitManager.getHeader(), params)
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });

    }
}
